﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class ams_Ams_general : System.Web.UI.Page
{
    public ArrayList companyarr;
    public ArrayList accountarr;
    public ArrayList employeearr;
    string company = "";
    string account = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["user"] == null) { Response.Redirect("../Index.aspx"); }
        if (!Page.IsPostBack)
        {
            try
            {
                if (Request.QueryString["company"] != null) company = Request.QueryString["company"].ToString();
                if (Request.QueryString["account"] != null) account = Request.QueryString["account"].ToString();
                //  todas la companies
                PAccount.Visible = false;
                PEmployee.Visible = false;
                PCompany.Visible = false;
                if (company == "" && account == "")
                {

                    Session["history"] = "Ams_general.aspx";
                    companyarr = (ArrayList)DB.getArrCompania("");
                    PCompany.Visible = true;
                    Session["company"] = null;
                    Session["account"] = null;
                    lb_account_id.Text = "0";
                    lb_company_id.Text = "0";



                }
                //listar  todas las accounts de la company seleccionada
                if (company != "" && account == "")
                {
                    accountarr = (ArrayList)DB.getArrCuenta(" where tcu_tco_id = " + company);
                    PAccount.Visible = true;
                    Session["history"] = "Ams_general.aspx?company=" + company;
                    Session["company"] = company;
                    Session["account"] = null;
                    lb_company_id.Text = company.ToString();
                    lb_account_id.Text = "0";
                }
                //listar el personal de la cuenta seleccionada
                if (company == "" && account != "")
                {
                    Session["history"]="Ams_general.aspx?account="+account;
                    Session["account"] = account;
                    
                    lb_account_id.Text = account.ToString();
                    lb_company_id.Text = company = DB.getCompanybyAccount(int.Parse(account)).ToString();
                        PEmployee.Visible = true;
                  
                        employeearr = (ArrayList)DB.getEmployementArr(" tbl_empleado.emp_tcu_id = " + account);

                   
                    ArrayList status=  DB.getArrStatus("");
                    TabContainer1.Tabs.RemoveAt(0);
                    foreach (RE_GenericBean rg in status) {
                         AjaxControlToolkit.TabPanel pane  =  new AjaxControlToolkit.TabPanel();
                         int c=getCountEmployeebyStatus(employeearr, rg.intC1);
                         pane.HeaderText = rg.strC1 + "&nbsp;<asp:Label ID='lb_" + rg.strC1 + "' runat='server' ForeColor='Blue'>("+c.ToString()+") </asp:Label>";
                        pane.ID= rg.strC1;
                        TabContainer1.Tabs.Add(pane);
                        TabContainer1.TabIndex = 0;
                        GridView gv = new GridView();
                        pane.Controls.Add(gv);
                        gv.ID = "gv_"+rg.strC1;
                        gv.Width = 850;
                       
                        DataSet ds = getDataSetEmployeebyStatus(employeearr, rg.intC1);
                        gv.DataSource = ds;
                        gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
                        gv.PageIndexChanging +=new GridViewPageEventHandler(gv_PageIndexChanging);
                        gv.PageSize = 25;
                        gv.EnableViewState = true;
                        gv.AllowPaging = true;
                        gv.PageIndex = 0;
                        gv.DataBind();
                        
                       
                    
                        
                       
                      
                        if (rg.intC1 == 1)
                        {
                            pane.Controls.Add(Button1);
                        }

                       
                        
                    }
                     

                }
            }
            catch (Exception err)
            {
                utility.log(err.StackTrace);
            }

        }
        else {
            if (Request.QueryString["company"] != null) { company = Request.QueryString["company"].ToString(); lb_company_id.Text = company; }
             if (Request.QueryString["account"] != null) account = Request.QueryString["account"].ToString();
             if (company == "" && account != "")
             {
                 Session["history"] = "Ams_general.aspx?account=" + account;
                 Session["account"] = account;
                 lb_account_id.Text = account.ToString();
              
                 PEmployee.Visible = true;
                 if (!tb_search.Text.Equals(""))
                 {
                     string temp = tb_search.Text.ToUpper();
                     employeearr = (ArrayList)DB.getEmployementArr("  (upper(tbl_persona.tper_nombre) like '%" + temp + "%' or upper(tbl_persona.tper_nombre2) like '%" + temp + "%' or upper(tbl_persona.tper_apellido) like '%" + temp + "%' or upper(tbl_persona.tper_apellido2) like '%" + temp + "%' ) ");

                     //tb_search.Text = "Buscando....";
                 }
                 else
                 {
                     employeearr = (ArrayList)DB.getEmployementArr(" tbl_empleado.emp_tcu_id = " + account);

                 }
               ArrayList status = DB.getArrStatus("");
               TabContainer1.Tabs.RemoveAt(0);
                 foreach (RE_GenericBean rg in status)
                 {
                     AjaxControlToolkit.TabPanel pane = new AjaxControlToolkit.TabPanel();
                     
                     int c = getCountEmployeebyStatus(employeearr, rg.intC1);
                     pane.HeaderText = rg.strC1 + "&nbsp;<asp:Label ID='lb_" + rg.strC1 + "' runat='server' ForeColor='Blue'>(" + c.ToString() + ") </asp:Label>";
                     pane.ID = rg.strC1;
                     TabContainer1.Tabs.Add(pane);
                     
                     GridView gv = new GridView();
                     gv.ID = "gv_" + rg.strC1;
                     gv.Width = 850;
                     pane.Controls.Add(gv);
                     DataSet ds = getDataSetEmployeebyStatus(employeearr, rg.intC1);
                     gv.DataSource = ds;
                     gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
                     gv.PageIndexChanging += new GridViewPageEventHandler(gv_PageIndexChanging);
                     gv.PageSize = 25;
                     gv.EnableViewState = true;
                     gv.AllowPaging = true;
                     gv.PageIndex = 0;
                  
                     gv.DataBind();
                     
                    


                    
                     if (rg.intC1 == 1)
                     {
                         pane.Controls.Add(Button1);
                     }
                 
                 }
                 
             }
        
        }
    }


    void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (sender != null)
        {
            
            GridView gv = (GridView)sender;
            gv.PageIndex = e.NewPageIndex;
            gv.DataBind();
        }
    }
    void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        try
        {
            string t = e.Row.RowType.ToString();
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[7].Visible = false;
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {

                e.Row.Attributes.CssStyle.Add("cursor", "hand");
                e.Row.Attributes.CssStyle.Add("background-color", "#d8dde0");
                e.Row.Attributes.CssStyle.Add("font-size","18px");
                e.Row.Attributes.CssStyle.Add("color", "#000000");
                e.Row.Attributes.CssStyle.Add("text-align","center");
                e.Row.Attributes.CssStyle.Add("letter-spacing","10px");
                e.Row.HorizontalAlign = HorizontalAlign.Center;
                

            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                string temp = e.Row.Cells[0].Text;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[7].Visible = false;
                e.Row.Height = 35;
                e.Row.BorderWidth = 1;
                e.Row.BorderColor = System.Drawing.Color.LightGray;
                HyperLink hy = new HyperLink();
                hy.Text = e.Row.Cells[1].Text;
                hy.NavigateUrl = "PersonalInfo.aspx?per_id=" + e.Row.Cells[0].Text;
                e.Row.Cells[1].Controls.Add(hy);
                int comp_id = 0;
                if (company != "")
                    comp_id = int.Parse(company);
                bool hasalert = utility.isCompletedProfile(int.Parse(e.Row.Cells[0].Text), comp_id, 2);
                e.Row.Cells[0].Style.Add("Style", "color:White!Important; ");
                if (!hasalert)
                {
                    Image image = new Image();
                    image.ImageUrl = "images/icons/exclamation.gif";
                    e.Row.Cells[0].Controls.Add(image);


                }
                else
                {
                    Image image = new Image();
                    image.ImageUrl = "images/blank.gif";
                    e.Row.Cells[0].Controls.Add(image);
                }

            }
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("Style", " width:40px;");
                e.Row.Cells[1].Attributes.Add("Style", " width:250px;");
                e.Row.Cells[2].Attributes.Add("Style", " width:250px;");
                e.Row.Cells[3].Attributes.Add("Style", " width:250px;");
                e.Row.Cells[4].Attributes.Add("Style", " width:250px;");
                e.Row.Cells[5].Attributes.Add("Style", " width:120px;");
                e.Row.Cells[6].Attributes.Add("Style", " width:100px;");
                e.Row.Cells[0].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[1].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[2].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[3].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[4].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[5].Attributes.Add("Style", " text-align: left;");
                e.Row.Cells[6].Attributes.Add("Style", " text-align: left;");
            }

        }catch(Exception ex){
            utility.log("Error rowdatabound employee "+ex.Message+ ex.StackTrace);
        }
    }

    

    private DataSet getDataSetEmployeebyStatus(ArrayList employeearr, int pstatus)
    {
        DataSet result = new DataSet();
        result.Tables.Add("Tabla");
        result.Tables[0].Columns.Add("Id");
        result.Tables[0].Columns.Add("Last Name");
        result.Tables[0].Columns.Add("First Name");
        result.Tables[0].Columns.Add("Middle Name");
        result.Tables[0].Columns.Add("Account");
        result.Tables[0].Columns.Add("Position");
        result.Tables[0].Columns.Add("Code");
        result.Tables[0].Columns.Add("Status Profile");
       
        foreach (RE_GenericBean r in employeearr)
        {

            if (r.intC2 == pstatus)
            {

                result.Tables[0].Rows.Add(r.intC1, r.strC3, r.strC1, r.strC2, r.strC5, r.strC6, r.strC4, r.intC2);
            }

        }
        return result;
    }

    private int getCountEmployeebyStatus(ArrayList employeearr, int pstatus)
    {
        int result = 0;
        foreach (RE_GenericBean r in employeearr) {
            string emp = r.intC1.ToString();
            

            if (r.intC2 == pstatus) {
                result++;
            }
        
        }





        return result;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["company"] != null)
        {
            RE_GenericBean comp = DB.getCompania(int.Parse(Session["company"].ToString()));
            
                Session["country"] = comp.intC2;
                Response.Redirect("PersonalInfo.aspx");
            
        }
        else {
            lb_company_id.Text = DB.getCompanybyAccount(int.Parse(lb_account_id.Text)).ToString();
            RE_GenericBean comp = DB.getCompania(int.Parse(lb_company_id.Text.ToString()));
            Session["account"] = lb_account_id.Text;
            Session["company"] = lb_company_id.Text;
                Session["country"] = comp.intC2;
                Response.Redirect("PersonalInfo.aspx");
            
          
        
        }
        
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        
        
        string url = Request.UrlReferrer.ToString();
        if (Session["company"] != null && Session["account"] != null)
        {
            url = "Ams_general.aspx?company=" + Session["company"].ToString();
        }
        else {
            if (Session["company"] != null && Session["account"] == null)
            {
                url = "Ams_general.aspx";

            }
            else {
                url = "Ams_general.aspx";
            
            }
        
        }
        Response.Redirect(url);
    }

   
}